package article

import (
	"go-gin-api/app/model"
	"go-gin-api/app/util"

	"github.com/gin-gonic/gin"
)

// AddTypesService 添加分类参数
type AddTypesService struct {
	TypeName string `json:"type_name"`
	Icon     string `json:"icon"`
	OrderNum string `json:"order_num"`
	Color    string `json:"color"`
}

// AddTypes 添加分类
func (s *AddTypesService) AddTypes(c *gin.Context) *util.Response {
	if s.TypeName == "" {
		return &util.Response{
			Code:    4000,
			Message: "failed",
			Data:    "分类名不能为空!",
		}
	}
	// 查询名称是否重复
	count := 0
	model.DB.Model(&model.ArticleType{}).Where("type_name=?", s.TypeName).Count(&count)
	if count > 0 {
		return &util.Response{
			Code:    4000,
			Message: "failed",
			Data:    "该分类存在!",
		}
	}
	types := model.ArticleType{
		TypeName: s.TypeName,
		Icon:     s.Icon,
		OrderNum: s.OrderNum,
		Color:    s.Color,
	}
	// 添加分类
	if err := model.DB.Create(&types).Error; err != nil {
		return &util.Response{
			Code:    4000,
			Message: "failed",
			Data:    "添加分类失败!",
		}
	}
	return &util.Response{
		Code:    200,
		Message: "success",
		Data:    "添加分类成功!",
	}
}
